package com.jeesite.modules.st.utils;

import com.jeesite.modules.st.config.RedisUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class BillUtil {
    public static final int none = 0;

    public enum BillType {
        /**
         * 采购入库
         */
        purchaseInBill("st_purchase_in_bill_no", "CGRK", 1),//采购入库
        purchaseOutBill("st_purchase_out_bill_no", "CGCK", 2),//采购出库
        inventoryLossBill("st_inventory_loss_bill_no", "PK", 11),//盘亏
        inventoryProfitBill("st_inventory_profit_bill_no", "PY", 12),//盘盈
        /**
         * 成品入库
         */
        productInBill("st_product_in_bill_no", "SCRK", 31),//生产入库，成品
        /**
         * 半成品入库
         */
        productHalfInBill("st_product_half_in_bill_no", "BCPRK", 32),//半成品
        /**
         * 销售出库
         */
        saleOutBill("st_sale_out_bill_no", "XSCK", 33),
        productMaterialBill("st_product_material_bill_no", "LL", 35),//生产领料
        purchaseBill("st_purchase_bill_no", "CG"),//采购
        sendNoticeBill("st_send_notice_bill_no", "SH"),//送货单
        /**
         * 进料检验
         */
        checkLogBill("st_check_log_bill_no", "JLJY"),//进料检验
        saleBill("st_sale_bill_no", "XS"),//销售单
        batchNo("batch_no", ""),//批号相关
        productBill("st_product_bill_no", "SC"),//生产订单
        takeStockBill("st_tack_stock_no", "PD"),//盘点单
        materialConvertBill("st_material_convert_no", "ZH", 40),//物料转换单
        otherOutBill("st_other_out_bill_no", "QTCK", 61),//其他出库
        breakOutBill("st_break_out_bill_no", "BFCK", 62),//报废出库
        supplyOutBill("st_supply_out_bill_no", "SCBL", 63),//生产补领

        ;
        String name;
        String prefix;
        int billCode;

        BillType(String name, String prefix, int billCode) {
            this.name = name;
            this.prefix = prefix;
            this.billCode = billCode;
        }

        BillType(String name, String prefix) {
            this.name = name;
            this.prefix = prefix;
        }

        public String getName() {
            return name;
        }


        public String getPrefix() {
            return prefix;
        }

        public int getBillCode() {
            return billCode;
        }

    }

    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    @Resource
    private RedisUtil redisUtil;

    public String billNo(BillType billType) {
        Integer b0 = (Integer) redisUtil.get(billType.getName());
        if (b0 == null) {
            redisUtil.set(billType.getName(), 0);
        }
        long b1 = redisUtil.incr(billType.getName(), 1);
        String format = sdf.format(new Date());
        String bf1 = String.format("%03d", b1);
        if (b1 > 999) {
            bf1 = String.format("%06d", b1);
        }
        String billNo = billType.getPrefix() + format + bf1;
        return billNo;
    }
}
